﻿using LD.Host;
using log4net.Core;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using YTE.Control;

namespace LD.BC
{
    public partial class FrmAxisParam : FrmBase
    {
        private ushort currentAxisId = 0;
        public FrmAxisParam()
        {
            InitializeComponent();
        }
        private void dgv_data_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                if (this.dgv_data.Rows.Count > 0)
                {
                    if (this.dgv_data["轴ID", e.RowIndex].Value != null)
                    {
                        //ushort id = Convert.ToUInt16(this.dgv_data["轴ID", e.RowIndex].Value);
                        //currentAxisId = id;
                        //var model = BcFactory.Instance?.Control.GetAlm(id);
                        //if (model != null)
                        //{
                        //    this.cob_alm使能.SelectedIndex = model.ALM使能;
                        //    this.cob_alm电平.SelectedIndex = model.ALM电平;
                        //}
                    }
                }
            }
        }
        private void btn_save_Click(object sender, EventArgs e)
        {
            var axis = BcFactory.Instance.Config.AxisParam.Where(p => p.AxisId == currentAxisId).FirstOrDefault();
            if (axis == null)
            {
                Log4.Warr("请先保存轴信息");
                return;
            }
            else
            {
                ////设置板卡
                //BcFactory.Instance.Control.SetAlm(currentAxisId, (ushort)this.cob_alm使能.SelectedIndex, (ushort)this.cob_alm电平.SelectedIndex);

                //var alm = BcFactory.Instance.Control.GetAlm(currentAxisId);
                //axis.AlmModel = alm;
            }
            BcFactory.Instance.Save();
        }

        private void FrmAxisParam_Load(object sender, EventArgs e)
        {
            Init();
        }
        private DataTable GetEnum<T>()
        {
            string[] str = Enum.GetNames(typeof(T));
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < str.Length; i++)
            {
                dt.Rows.Add(i, str[i]);
            }
            return dt;
        }
        private void Init()
        {
            //axis
            if (BcFactory.Instance.Config != null)
                foreach (var item in BcFactory.Instance.Config.AxisParam)
                {
                    this.dgv_data.Rows.Add(item.AxisId, item.Name);
                }

            FrmIoSet ioset = new FrmIoSet();
            ioset.Dock = DockStyle.Fill;
            ioset.TopLevel = false;
            ioset.Show();
            this.tabPage1.Controls.Add(ioset);

            FrmPointSet pointSet = new FrmPointSet();
            pointSet.Dock = DockStyle.Fill;
            pointSet.TopLevel = false;
            pointSet.Show();
            this.tabPage2.Controls.Add(pointSet);
        }

        private void btn_saveAxis_Click(object sender, EventArgs e)
        {
            //保存轴
            foreach (DataGridViewRow item in this.dgv_data.Rows)
            {
                if (item.Cells["轴名称"].Value != null && item.Cells["轴ID"].Value != null)
                {
                    if (BcFactory.Instance.Config?.AxisParam == null)
                        BcFactory.Instance.Config.AxisParam = new List<TB_AxisParam>();

                    ushort id = Convert.ToUInt16(item.Cells["轴ID"].Value);
                    var axis = BcFactory.Instance.Config.AxisParam.Where(p => p.AxisId == id).FirstOrDefault();
                    if (axis == null)
                    {
                        var axisModel = new TB_AxisParam();
                        axisModel.AxisId = id;
                        axisModel.Name = item.Cells["轴名称"].Value.ToString();
                        BcFactory.Instance.Config.AxisParam.Add(axisModel);
                    }
                    else
                    {
                        axis.Name = item.Cells["轴名称"].Value.ToString();
                    }
                }
            }
            BcFactory.Instance.Save();
        }
    }
}
